package com.tjec.project.businessStandardLibrary.controller;

import java.util.List;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeBusinessGuide;
import com.tjec.project.businessStandardLibrary.service.IBmsKnowledgeBusinessGuideService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 业务手册Controller
 * 
 * @author pangyongfeng
 * @date 2020-04-20
 */
@Api("业务手册")
@RestController
@RequestMapping("/library/guide")
public class BmsKnowledgeBusinessGuideController extends BaseController
{
    @Autowired
    private IBmsKnowledgeBusinessGuideService bmsKnowledgeBusinessGuideService;
    @Value("${sftp.http}")
    private String http;
    @Value("${sftp.ip}")
    private String ip;

    /**
     * 查询业务手册列表
     */
    @ApiOperation("查询业务手册列表")
    @PreAuthorize("@ss.hasPermi('library:guide:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
        startPage();
        List<BmsKnowledgeBusinessGuide> list = bmsKnowledgeBusinessGuideService.selectBmsKnowledgeBusinessGuideList(bmsKnowledgeBusinessGuide);
        for (BmsKnowledgeBusinessGuide knowledgeBusinessGuide : list) {
            addResourcePrefix(knowledgeBusinessGuide);
        }
        return getDataTable(list);
    }

    /**
     * 导出业务手册列表
     */
    @ApiOperation("导出业务手册列表")
    @PreAuthorize("@ss.hasPermi('library:guide:export')")
    @Log(title = "业务指南", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
        List<BmsKnowledgeBusinessGuide> list = bmsKnowledgeBusinessGuideService.selectBmsKnowledgeBusinessGuideList(bmsKnowledgeBusinessGuide);
        ExcelUtil<BmsKnowledgeBusinessGuide> util = new ExcelUtil<BmsKnowledgeBusinessGuide>(BmsKnowledgeBusinessGuide.class);
        return util.exportExcel(list, "guide");
    }

    /**
     * 获取业务手册详细信息
     */
    @ApiOperation("获取业务手册详细信息")
    //@PreAuthorize("@ss.hasPermi('library:guide:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide = bmsKnowledgeBusinessGuideService.selectBmsKnowledgeBusinessGuideByGuid(guid);
        addResourcePrefix(bmsKnowledgeBusinessGuide);
        return AjaxResult.success(bmsKnowledgeBusinessGuide);
    }

    /**
     * 新增业务手册
     */
    @ApiOperation("新增业务手册")
    @PreAuthorize("@ss.hasPermi('library:guide:add')")
    @Log(title = "业务指南", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
        return toAjax(bmsKnowledgeBusinessGuideService.insertBmsKnowledgeBusinessGuide(bmsKnowledgeBusinessGuide));
    }

    /**
     * 修改业务手册
     */
    @ApiOperation("修改业务手册")
    @PreAuthorize("@ss.hasPermi('library:guide:edit')")
    @Log(title = "业务指南", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
        System.out.println(bmsKnowledgeBusinessGuide.toString());
        return toAjax(bmsKnowledgeBusinessGuideService.updateBmsKnowledgeBusinessGuide(bmsKnowledgeBusinessGuide));
    }

    /**
     * 删除业务手册
     */
    @ApiOperation("删除业务手册")
    @PreAuthorize("@ss.hasPermi('library:guide:remove')")
    @Log(title = "业务指南", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(bmsKnowledgeBusinessGuideService.deleteBmsKnowledgeBusinessGuideByGuids(guids));
    }

    /**
     * 查询业务手册 带上树
     */
    @ApiOperation("查询业务手册列表条件")
    @PreAuthorize("@ss.hasPermi('library:guide:list')")
    @GetMapping("/selectList")
    public TableDataInfo selectList(BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide)
    {
        startPage();
        List<BmsKnowledgeBusinessGuide> list = bmsKnowledgeBusinessGuideService.selectBmsKnowledgeBusinessGuideselectList(bmsKnowledgeBusinessGuide);
        for (BmsKnowledgeBusinessGuide knowledgeBusinessGuide : list) {
            addResourcePrefix(knowledgeBusinessGuide);
        }
        return getDataTable(list);
    }
    /**
     * 查询业务手册 详情
     */
    @ApiOperation("业务手册获取详情")
    //@PreAuthorize("@ss.hasPermi('library:guide:query')")
    @GetMapping("/details")
    public AjaxResult selectListDetails(String guid)
    {
        BmsKnowledgeBusinessGuide bmsKnowledgeBusinessGuide = bmsKnowledgeBusinessGuideService.selectBmsKnowledgeBusinessGuideselectListDetails(guid);
        addResourcePrefix(bmsKnowledgeBusinessGuide);
       return AjaxResult.success("操作成功", bmsKnowledgeBusinessGuide);
    }

    /**
     * 附件路径添加前缀
     * @param knowledgeBusinessGuide
     * @return
     */
    private void addResourcePrefix(BmsKnowledgeBusinessGuide knowledgeBusinessGuide){
        if(knowledgeBusinessGuide!=null&& StringUtils.isNotBlank(knowledgeBusinessGuide.getStoragePath())){
            knowledgeBusinessGuide.setStoragePath(http + ip +Constants.RESOURCE_PREFIX + knowledgeBusinessGuide.getStoragePath());
        }
    }
}
